<?php
/**
 * (jobs) 物流运输天数
 */
namespace App\Jobs\Basics;

use App\Models\Shipping\SSku;
use App\Models\SsBasic\AreaSku;
use App\Models\Stocking\StockingSku;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class TransportDayRelate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $areaId;
    protected $airDay;
    protected $seaDay;

    /**
     * TransportDayRelate constructor.
     * @param $areaId
     * @param $airDay
     * @param $seaDay
     */
    public function __construct($areaId,$airDay,$seaDay)
    {
        $this->areaId = $areaId;
        $this->airDay = $airDay;
        $this->seaDay = $seaDay;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->areaId,$this->airDay,$this->seaDay);
    }

    //更新备货，提货
    private function exeFillData($areaId,$airDay,$seaDay){
        StockingSku::query()->where('area_id',$areaId)->orderBy('id')
            ->chunk(500,function ($item) use ($airDay,$seaDay){
            foreach ($item as $_item){
                $_item->air_day = $airDay;
                $_item->air_available = AreaSku::getAvailable($_item->purchase_cycle,$_item->air_day);
                $_item->air_ship_factor = AreaSku::getShipFactor($_item->air_day);
                $_item->air_stock_factor = AreaSku::getStockFactor($_item->purchase_cycle,$_item->air_day);
                $_item->sea_day = $seaDay;
                $_item->sea_available = AreaSku::getAvailable($_item->purchase_cycle,$_item->sea_day);
                $_item->sea_ship_factor = AreaSku::getShipFactor($_item->sea_day);
                $_item->sea_stock_factor = AreaSku::getStockFactor($_item->purchase_cycle,$_item->sea_day);
                $_item->save();
                $_item->over_stock = StockingSku::getOverStock($_item);
                $_item->over_stock_value = StockingSku::getOverStockValue($_item->over_stock,$_item->unit_price);
                $_item->save();
            }
        });
        usleep(10);
        SSku::query()->where('area_id',$areaId)->orderBy('id')
            ->chunk(500,function ($item) use ($airDay,$seaDay){
                foreach ($item as $_item){
                    $_item->air_day = $airDay;
                    $_item->air_factor = AreaSku::getSSkuFactor($_item->purchase_cycle,$_item->air_day);
                    $_item->sea_day = $seaDay;
                    $_item->sea_factor = AreaSku::getSSkuFactor($_item->purchase_cycle,$_item->sea_day);
                    $_item->save();
                }
            });
    }
}
